package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.data.dto.FollowUpDetailsDto;
import org.gov.vis.data.gui.AddProgencyDialog;
import org.gov.vis.data.gui.AddInsertFollowUpDetailsDialog;
import org.gov.vis.data.gui.AddUpdateFollowUpDetailsDialog;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DU;
import org.gov.vis.util.Util;

public class AddFollowUpDetailsListener  implements ActionListener,BaseListener{
	
	private AddProgencyDialog addProgencyDialog;
	final private ResourceBundle rb = Util.getResources();
	private AddInsertFollowUpDetailsDialog insertFollowUpDetailsDialog;
	private AddUpdateFollowUpDetailsDialog updateFollowUpDetailsDialog;


	public AddFollowUpDetailsListener(AddProgencyDialog addProgencyDialog){

		this.addProgencyDialog = addProgencyDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		final FollowUpDetailsDto followUpDetailsDto = (FollowUpDetailsDto)_dtoObject;
		if(updateFollowUpDetailsDialog != null){
			updateFollowUpDetailsDialog.getTxtBxOthers().setText(followUpDetailsDto.getOther());
			updateFollowUpDetailsDialog.getTxtBxBirthWeight().setText(followUpDetailsDto.getBirthWeight());
			updateFollowUpDetailsDialog.getTxtBxMonthlyWt().setText(followUpDetailsDto.getMonthlyWeight());
			updateFollowUpDetailsDialog.getTxtBxNutrition().setText(followUpDetailsDto.getNutrition());
		System.out.println(followUpDetailsDto.getDeWormingDate());
		
		updateFollowUpDetailsDialog.getDtChoDateOfDeworming().setDate(followUpDetailsDto.getDeWormingDate());
		updateFollowUpDetailsDialog.getDtChoDateOfDeticking().setDate(followUpDetailsDto.getDeTickingDate());
		updateFollowUpDetailsDialog.getDtChoDateOfVaccination().setDate(followUpDetailsDto.getVaccinationDate());
			
		}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		try{
			

			if( actionEvent.getActionCommand().equals("Add new Follow Up Details") ){
				insertFollowUpDetailsDialog = new AddInsertFollowUpDetailsDialog(Main.getMainFrame(), "Follow UP Details", false);
				insertFollowUpDetailsDialog.setProgencyObject(addProgencyDialog);
			}
			if(actionEvent.getSource() instanceof VISHyperLink){
				actionEvent.getSource();
				VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
				FollowUpDetailsDto followUpDetailsDto = (FollowUpDetailsDto)visHyperLink.getObject();
				System.out.println("action from the object----"+followUpDetailsDto.getBirthWeight());
				updateFollowUpDetailsDialog = new AddUpdateFollowUpDetailsDialog(Main.getMainFrame(), rb.getString("dialog_edit_prmt_semen_title"), false);
				updateFollowUpDetailsDialog.setProgencyObject(addProgencyDialog);
				updateFollowUpDetailsDialog.setFollowUpDetailsObject(followUpDetailsDto);
				setDtoDetailsToGui(followUpDetailsDto);
			
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	public void getFollowUpDetails(){
		//Get all the follow up Details
		
		List<String> colLst = new ArrayList<String>();
		colLst.add("Deworming");
		colLst.add("Vaccination");
		colLst.add("Deticking");
		colLst.add("Others");
		colLst.add("Nutrition");
		colLst.add("Birth Wt");
		colLst.add("Monthly Wt");

		/*FollowUpDetailsDto followUpDetDto1 = new FollowUpDetailsDto();
		followUpDetDto1.setDeWormingDate(new Date());
		followUpDetDto1.setVaccinationDate(new Date());
		followUpDetDto1.setDeTickingDate(new Date());
		followUpDetDto1.setOther("Others");
		followUpDetDto1.setNutrition("Nutrition");
		followUpDetDto1.setBirthWeight("birth");
		followUpDetDto1.setMonthlyWeight("monthly");
		
		FollowUpDetailsDto followUpDetDto2 = new FollowUpDetailsDto();
		followUpDetDto2.setDeWormingDate(new Date());
		followUpDetDto2.setVaccinationDate(new Date());
		followUpDetDto2.setDeTickingDate(new Date());
		followUpDetDto2.setOther("Others2");
		followUpDetDto2.setNutrition("Nutrition2");
		followUpDetDto2.setBirthWeight("birth2");
		followUpDetDto2.setMonthlyWeight("monthly2");
		*/
		
		/*List<FollowUpDetailsDto> followUpLst = new ArrayList<FollowUpDetailsDto>();
		followUpLst.add(followUpDetDto1);
		followUpLst.add(followUpDetDto2);
		*/
		
		List<FollowUpDetailsDto> followUpLst = AddProgencyDialog.tempFollowUpLst;
		ArrayList<Object> dataList = new ArrayList<Object>(1);
		if(followUpLst != null && !followUpLst.isEmpty()){
			for(FollowUpDetailsDto dto:followUpLst){
				ArrayList<Object> lst = new ArrayList<Object>(1);
				//setting each property of the current Accessory object (row) to the list
				lst.add(dto);
				lst.add(DU.format(dto.getDeWormingDate()));
				lst.add(DU.format(dto.getVaccinationDate()));
				lst.add(DU.format(dto.getDeTickingDate()));
				lst.add(dto.getOther());
				lst.add(dto.getNutrition());
				lst.add(dto.getBirthWeight());
				lst.add(dto.getMonthlyWeight());
				dataList.add(lst);
			}//end for 
		}//end if
		VISDataGrid.createDataPanel(dataList, colLst, addProgencyDialog.getPnlFollowUpDetails(),this);
		addProgencyDialog.getTxtBxFarmerCode().requestFocus();
		
	}
	
}
